<?php
namespace app\index\controller;


use think\Controller;
use app\index\model\User as userModel;

class User extends Controller{
	//用户注册
	public function regist(){
		//实例化User
		//接收前端表单提交的数据
		$user = new User();
		$user->name = input('post.UserName');
		$user->phone = input('post.UserTel');
		$user->depart = $_POST['Depart'];
		$question = input('post.question');
		$user_id;
		//进行规则验证
		$result = $this->validate(
				[
						'name' => $user->name,
						'phone' => $user->phone,
						'depart' => $user->depart,
				],
				[
						'name' => 'require|max:20',
						'phone' => 'require',
						'depart' => 'require',
				]);
		if (true !== $result) {
			$this->error($result);
		} else {
			// 查询是否存在
			$user2 = UserModel::get(['phone'=>input('post.UserTel')]);
			$is_new = 10;
			
			// 不存在则注册一个
			if( null === $user2) {
				$user2 = $user;
				$user2->point = 10;
				if($question == 1){
					$user2->qr1 = 10;
				} else if($question == 2){
					$user2->qr2 = 10;
				} else if($question == 3){
					$user2->qr3 = 10;
				} else if($question == 4){
					$user2->qr4 = 10;
				} else if($question == 5){
					$user2->qr5 = 10;
				} else {
					$user2->qr6 = 10;
				}
				//写入数据库
				$user3 = UserModel::create($user2);
				if ($user3 != null) {
						$user_id = $user3->id;
				} else {
					return $this->success('注册失败');
				}
			} else {
				$is_new = 0;
				if($question == 1 && $user2->qr1 < 10){
					$user2->qr1 = 10;
					$is_new = 10;
				} else if($question == 2 && $user2->qr2 < 10){
					$user2->qr2 = 10;
					$is_new = 10;
				} else if($question == 3 && $user2->qr3 < 10){
					$user2->qr3 = 10;
					$is_new = 10;
				} else if($question == 4 && $user2->qr4 < 10){
					$user2->qr4 = 10;
					$is_new = 10;
				} else if($question == 5 && $user2->qr5 < 10){
					$user2->qr5 = 10;
					$is_new = 10;
				} else if($question == 6 && $user2->qr6 < 10){
					$user2->qr6 = 10;
					$is_new = 10;
				} else {
					$is_new = 0;
				}
				$user2->point += $is_new;
				$user2->depart = $user->depart;
				$user2->save();
				$user_id = $user2->id;
			}
			
			$this -> view -> assign('is_new', $is_new);
			$this -> view -> assign('question', input('post.question'));
			$this -> view -> assign('id', $user_id);
			return $this -> view -> fetch('point');
			
		}
		
		
		
		

	}
	
	//跳转答题页面
	public function toquestion() {
		
		$this -> view -> assign('question', input('post.question'));

		$this -> view -> assign('id', input('post.id'));
		
		return $this -> view -> fetch('question');
	}
	
	//跳转答案界面
	public function toanswer() {
		$question = input('post.question');
		$user_id = input('post.id');
		$status = 0; //验证失败标志
		$score = 0;
		$result = '回答错误！'; //失败提示信息
		$user2 = UserModel::get(['id'=>$user_id]);
		$data = [
				'answer1' => input('post.answer1'),
				'answer2' => input('post.answer2'),
				'answer3' => input('post.answer3'),
				'answer4' => input('post.answer4'),
				'answer5' => input('post.answer5'),
				'answer6' => input('post.answer6'),
		];
		if($question == 1){
			if($data['answer1'] == 'C'){
				$status = 1;		
				if($user2-> qr1 < 30){
					$score += 10;
					$user2-> qr1 += $score;
				}
			}
			if($data['answer6'] == 'B'){
				$status = 1;
				if($user2-> qr1 < 30){
					$score += 10;
					$user2-> qr1 += $score;
				}
			}

			$user2->point += $score;
			$user2->save();
			
			
		} else if($question == 2 && $data['answer2'] == 'A'){
					$status = 1;
					if($user2 -> qr2 == 10) {
						$score = 10;
						$user2-> qr2 += $score;
						$user2->point += $score;
						$user2->save();
					}
		} else if($question == 3 && $data['answer3'] == 'B'){
					$status = 1;
					if($user2 -> qr3 == 10) {
						$score = 10;
						$user2-> qr3 += $score;
						$user2->point += $score;
						$user2->save();
					}
		} else if($question == 4 && $data['answer4'] == 'C'){
					$status = 1;
					if($user2 -> qr4 == 10) {
						$score = 10;
						$user2-> qr4 += $score;
						$user2->point += $score;
						$user2->save();
					}
		} else if($question == 5 && $data['answer5'] == 'C'){
					$status = 1;
					if($user2 -> qr5 == 10) {
						$score = 10;
						$user2-> qr5 += $score;
						$user2->point += $score;
						$user2->save();
					}
		}
		
		$this -> view -> assign('score', $score);
		$this -> view -> assign('name', $user2->name);
		$this -> view -> assign('phone', $user2->phone);
		$this -> view -> assign('status', $status);
		$this -> view -> assign('id', $user_id);
		$this -> view -> assign('point', $user2->point);
		return $this -> view -> fetch('result');
	
	}
	
	// 跳转成绩界面
	public function torank() {
		$list = UserModel::scope(function($query){
			$query->where('id','>',0)->order('depart,point desc');
		})->select();
		
		
		$this -> view -> assign('list', $list);
		
		return $this -> view -> fetch('rank');
	}
	
	// 测试方法
	public function index() {
	
		$where = function($query) {
			$query -> where('id','>','0')
			-> order('depart,point desc');
		};
		$list = userModel::all($where);
	
		foreach ($list as $value){
				
			dump($value->getData());
		}
	}
	
	
	
	
	
	
}
	